<template>
  <div class="form-container">
    <el-form inline :model="form" ref="refForm" class="form-line" label-width="80">
      <el-form-item label="使用范围" prop="useScope">
        <el-select @change="getFeedbackTypeList" v-model="form.useScope" placeholder="请选择反馈来源">
          <el-option label="攸诺城配" value="1"></el-option>
          <el-option label="攸诺运管" value="2"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div class="btn-container">
      <!-- <el-button type="primary" @click="handleReset(refForm as FormInstance)">重置</el-button> -->
      <el-button type="primary" @click="handleAdd">添加类型</el-button>
    </div>
  </div>
  <div class="table-container">
    <el-table
      border
      :cell-style="{ textAlign: 'center', fontSize: '13px', padding: '0' }"
      :header-cell-style="{
        textAlign: 'center',
        fontWeight: '100',
        fontSize: '13px',
        color: 'white',
        backgroundColor: '#009688'
      }"
      style="width: 100%"
      :data="tableData"
    >
      <el-table-column fixed prop="parent.useScope" label="反馈来源">
        <template #default="scope">
          {{ scope.row.parent.useScope === "1" ? "攸诺城配" : "攸诺运管" }}
        </template>
      </el-table-column>
      <el-table-column fixed prop="parent.name" label="父级类型" />
      <el-table-column fixed prop="child" label="反馈类型" class="child-table">
        <template #default="scope">
          <el-table
            :cell-style="{ textAlign: 'center', fontSize: '13px', padding: '12px' }"
            :header-cell-style="{
              textAlign: 'center',
              fontWeight: '100',
              fontSize: '13px',
              color: 'white',
              backgroundColor: '#009688'
            }"
            :show-header="false"
            style="width: 100%"
            :data="scope.row.child"
          >
            <el-table-column prop="name" label="反馈类型" />
          </el-table>
        </template>
      </el-table-column>
      <el-table-column fixed prop="child" label="创建时间" class="child-table">
        <template #default="scope">
          <el-table
            :cell-style="{ textAlign: 'center', fontSize: '13px', padding: '12px' }"
            :header-cell-style="{
              textAlign: 'center',
              fontWeight: '100',
              fontSize: '13px',
              color: 'white',
              backgroundColor: '#009688'
            }"
            :show-header="false"
            style="width: 100%"
            :data="scope.row.child"
          >
            <el-table-column prop="createTime" label="创建时间" />
          </el-table>
        </template>
      </el-table-column>
      <el-table-column fixed prop="child" label="操作" width="250" class="child-table">
        <template #default="scope">
          <el-table
            :cell-style="{ textAlign: 'center', fontSize: '13px', padding: '11.5px' }"
            :header-cell-style="{
              textAlign: 'center',
              fontWeight: '100',
              fontSize: '13px',
              color: 'white',
              backgroundColor: '#009688'
            }"
            :show-header="false"
            style="width: 100%"
            :data="scope.row.child"
          >
            <el-table-column prop="createTime" label="操作">
              <template #default="childScope">
                <el-button type="text" size="small" @click="handleEdit(scope.row, childScope.row)">修改</el-button>
                <el-button type="text" size="small" @click="handleDelete(childScope.row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-table-column>
      <!-- <el-table-column prop="feedbackType" label="反馈类型" />
      <el-table-column prop="status" label="状态" />
      <el-table-column prop="replyTime" label="反馈时间" /> -->
      <!-- <el-table-column fixed="right" label="操作">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="handleDelete(scope.row.id)">删除</el-button>
          <el-button link type="primary" size="small" @click="handleEdit(scope.row.id)"> 修改 </el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <div class="pagination-container">
      <el-pagination
        background
        size="small"
        @change="getFeedbackTypeList"
        layout="prev, pager, next"
        :total="pageTotal"
        :page-size="form.pageSize"
        v-model:current-page="form.pageNum"
      />
    </div>
  </div>
  <el-dialog v-model="centerDialogVisible" :title="btnStatus ? '添加类型' : '修改类型'" width="500" center>
    <div>
      <el-form :model="updateForm" ref="refUpdateForm" :rules="updateFormRules" label-width="100">
        <el-form-item prop="useScope" label="使用范围">
          <el-select @change="selectList" v-model="updateForm.useScope" placeholder="请选择反馈来源">
            <el-option label="攸诺城配" value="1"></el-option>
            <el-option label="攸诺运管" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="parentId" label="父级类型">
          <el-select v-model="updateForm.parentId" placeholder="请选择父级类型">
            <el-option
              v-for="item in selectOptions"
              :key="item.parent.id"
              :label="item.parent.name"
              :value="item.parent.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="name" label="反馈类型">
          <el-input v-model="updateForm.name" placeholder="请输入反馈类型"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleUpdateSubmit(refUpdateForm as FormInstance)">{{
          btnStatus ? "添加" : "修改"
        }}</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="tsx" name="useProTable">
import { BaseType } from "@/api/interface/province/base";
import {
  addFeedbackTypeAPI,
  deleteFeedbackTypeAPI,
  getFeedbackTypeListAPI,
  updateFeedbackTypeAPI
} from "@/api/modules/province/baseAPI";
import { ElMessage, ElMessageBox, FormInstance, FormRules } from "element-plus";
import { onMounted, ref } from "vue";
const centerDialogVisible = ref<boolean>(false);
let pageTotal = ref<number>(0);
const form = ref<{
  useScope: string;
  pageSize: number;
  pageNum: number;
}>({
  useScope: "",
  pageSize: 10,
  pageNum: 1
});
const updateForm = ref<BaseType.UpdateParams>({
  name: "",
  useScope: ""
});
const refUpdateForm = ref<FormInstance>();
const updateFormRules = ref<FormRules>({
  name: [
    { required: true, message: "请输入反馈类型", trigger: "blur" },
    { min: 2, max: 50, message: "长度在 2 到 50 个字符", trigger: "blur" }
  ],
  useScope: [{ required: true, message: "请选择使用范围", trigger: "blur" }]
});
const refForm = ref<FormInstance>();
const tableData = ref<BaseType.Datum[]>([]);
const btnStatus = ref<boolean>(true);
const handleAdd = () => {
  centerDialogVisible.value = true;
  btnStatus.value = true;
  refUpdateForm.value = refForm.value;
  updateForm.value = {
    name: "",
    // parentId: -2147483648,
    useScope: ""
  };
};
const handleEdit = (parRow: BaseType.Datum, row: BaseType.Child) => {
  console.log(parRow);
  console.log(row);
  centerDialogVisible.value = true;
  btnStatus.value = false;
  refUpdateForm.value = refForm.value;
  updateForm.value = {
    name: row.name,
    id: row.id,
    parentId: parRow.parent.id,
    useScope: row.useScope
  };
};
const handleUpdateSubmit = (formEl: FormInstance) => {
  formEl.validate().then(() => {
    if (btnStatus.value) {
      addFeedbackType();
    } else {
      updateFeedbackType();
    }
  });
};
const handleDelete = (id: string) => {
  ElMessageBox.confirm("确认删除该类型？")
    .then(() => {
      deleteFeedbackType(id);
    })
    .catch(() => {
      ElMessage.info("已取消删除");
    });
};
const selectOptions = ref<BaseType.Datum[]>([]);
const selectList = async () => {
  const { code, data } = await getFeedbackTypeListAPI({
    useScope: updateForm.value.useScope
  });
  if (code == "200" && data) {
    selectOptions.value = data;
  }
};
const getFeedbackTypeList = async () => {
  const { code, data, total } = await getFeedbackTypeListAPI(form.value);
  if (code == "200") {
    if (data) {
      tableData.value = data;
    } else {
      tableData.value = [];
    }
    if (total) {
      pageTotal.value = total;
    }
  }
};
const addFeedbackType = async () => {
  const { code, msg } = await addFeedbackTypeAPI(updateForm.value);
  if (code == "200") {
    ElMessage.success("添加成功");
    centerDialogVisible.value = false;
    getFeedbackTypeList();
  } else {
    ElMessage.error(msg);
  }
};
const updateFeedbackType = async () => {
  const { code, msg } = await updateFeedbackTypeAPI(updateForm.value);
  if (code == "200") {
    ElMessage.success("修改成功");
    centerDialogVisible.value = false;
    getFeedbackTypeList();
  } else {
    ElMessage.error(msg);
  }
};
const deleteFeedbackType = async (ids: string) => {
  const { code, msg } = await deleteFeedbackTypeAPI(ids);
  if (code == "200") {
    ElMessage.success("删除成功");
    centerDialogVisible.value = false;
    getFeedbackTypeList();
  } else {
    ElMessage.error(msg);
  }
};
onMounted(() => {
  getFeedbackTypeList();
  selectList();
});
</script>
<style lang="scss" scoped>
.form-container {
  padding: 10px;
  margin-bottom: 10px;
  background-color: white;
  .btn-container {
    display: inline-block;
    float: right;
    vertical-align: middle;
  }
  .el-form {
    display: inline-block;
  }
  .form-line {
    .el-form-item {
      margin: 0;
      margin-right: 20px;
    }
    .el-input {
      width: 240px;
    }
    .el-select {
      width: 240px;
    }
  }
}
.table-container {
  padding: 40px 10px;
  padding-bottom: 60px;
  background-color: white;
  .pagination-container {
    // display: inline-block;
    float: right;
    margin-top: 20px;
    background-color: white;
  }
}
::v-deep .el-table .cell {
  padding: 0;
}
::v-deep .el-table .cell:hover {
  background-color: transparent;
}

// ::v-deep .el-table .el-table__cell {
//   padding: 0;
// }
::v-deep .el-form-item__label {
  font-size: 13px;
  font-weight: 100;
  color: black;
}
::v-deep .el-button > span {
  font-size: 13px;
}
::v-deep .el-input__inner {
  font-size: 13px;
}
::v-deep .el-select__wrapper {
  font-size: 13px;
}
:v-deep .el-radio__label {
  font-size: 13px;
}
</style>
